<?php
namespace app\admin\controller;
use think\Db;
use think\facade\{
    View,Env,Config
};
class Btcplat extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [['and','id','>',0]];
            if($d['keyword']!='') $map[] = ['and','title|coins','like', "%{$d['keyword']}%"];
            $w = where($map);
            $rs = $this->db->query("select * from btc_plat $w[0] order by sort desc,id desc".limit($d['page'],$d['limit']),$w[1],'',true );
            if($rs){
                suc($rs[0], $rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            unset($d['file']);
            $id = $this->db->insert('btc_plat',$d);
            m('Attachment','xcom')->imgSure('btc_plat',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                suc();
            }else{
                err('添加失败');
            }
        }
        return $this->fetch();
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'title' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            unset($d['file']);
            $id = $this->db->update('btc_plat',$d);
            m('Attachment','xcom')->imgSure('btc_plat',$d['id'],$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $id = $this->get['id'];
        $info = $this->db->query("select * from btc_plat where id=?",[$id],1);
        return $this->fetch(['info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('btc_plat',$d['id']);
            m('Attachment','xcom')->imgDel('btc_plat',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('btc_plat',$id);
                m('Attachment','xcom')->imgDel('btc_plat',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }
    //详情
    public function detail($id){
        return $this->fetch();
    }
}
